//
//  MyDbTools.m
//  jishiben
//
//  Created by 李钰 on 2017/11/27.
//  Copyright © 2017年 jishiben. All rights reserved.
//

#import "MyDbTools.h"
#import "NSThread+Extend.h"
#import "FMDB.h"
#import "Dbsql.h"
@implementation MyDbTools
{
    FMDatabase *_fmdb;
    FMDatabaseQueue *_fmdbQueue;
}
+ (MyDbTools *)sharedInstance {
    static id myDbTools = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        myDbTools = [[self alloc] initPrivate];
    });
    return myDbTools;
}
- (instancetype)initPrivate {
    if (self = [super init]) {
        NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",@"MyHistory.sqlite"];
        _fmdbQueue = [FMDatabaseQueue databaseQueueWithPath: filePath];
        _fmdb = [FMDatabase databaseWithPath: filePath];
        [_fmdb open];
        [_fmdb executeUpdate: MyHistoryCreate]; //用户食物表
        [_fmdb close];
    }
    return self;
}
///////
- (void)insertObj:(MoodObj *)obj Success:(void (^)(BOOL))success
{
    [NSThread runInBackground:^{
        [_fmdbQueue inDatabase:^(FMDatabase *db) {
            [db open];
            NSArray * paramArr = @[@(obj.mood),obj.creatDate,@(obj.creatTime),obj.doText,@(obj.status),@(obj.weather)];
            BOOL flag = [db executeUpdate: MyHistoryInsert withArgumentsInArray: paramArr];
            [db close];
            [NSThread runInMain:^{
                success(flag);
            }];
        }];
    }];
    
}
- (void)deleteId:(NSInteger)Id Success:(void (^)(BOOL))success
{
    [NSThread runInBackground:^{
        [_fmdbQueue inDatabase:^(FMDatabase *db) {
            [db open];
            BOOL flag = [db executeUpdate: MyHistoryUpdate withArgumentsInArray: @[@(Id)]];
            [db close];
            [NSThread runInMain:^{
                success(flag);
            }];
        }];
    }];
}
- (void)getListWithStatus:(NSInteger)status Success:(void (^)(NSArray *))success
{
    [NSThread runInBackground:^{
        [_fmdbQueue inDatabase:^(FMDatabase *db) {
            [db open];
            NSString * sql = [NSString stringWithFormat: @"select * from myHistory where status = %zi order by creatTime desc",status];
            FMResultSet * result = [db executeQuery: sql];
            NSArray * dataSource = [self getArrWithResult: result];
            
            [result close];
            result = nil;
            [db close];
            
            [NSThread runInMain:^{
                success(dataSource);
            }];
        }];
    }];
}
- (NSArray *)getArrWithResult:(FMResultSet *)result
{
    NSMutableArray * list = [NSMutableArray new];
    NSMutableArray * dateArr = [NSMutableArray new];
    while ([result next])
    {
        NSDictionary * dic = [result resultDictionary];
        MoodObj * obj = [[MoodObj alloc] initWithDic: dic];
        [list addObject: obj];
        if (![dateArr containsObject: obj.creatDate]) {
            [dateArr addObject: obj.creatDate];
        }
    }
    NSMutableArray * dataSource = [NSMutableArray new];
    for (NSString * date in dateArr) {
        NSMutableArray * source = [NSMutableArray new];
        for (MoodObj * obj in list) {
            if ([date isEqualToString: obj.creatDate]) {
                [source addObject: obj];
            }
        }
        [dataSource addObject: source];
    }
    return dataSource;
}
- (void)getListWithMood:(NSInteger)mood Success:(void (^)(NSArray *))success
{
    [NSThread runInBackground:^{
        [_fmdbQueue inDatabase:^(FMDatabase *db) {
            [db open];
            NSString * sql = [NSString stringWithFormat: @"select * from myHistory where mood = %zi order by creatTime desc",mood];
            
            FMResultSet * result = [db executeQuery: sql];
            NSArray * dataSource = [self getArrWithResult: result];
            [result close];
            result = nil;
            [db close];
            
            [NSThread runInMain:^{
                success(dataSource);
            }];
        }];
    }];
}
////今天
- (void)getTodayListSuccess:(void (^)(NSArray *))success
{
    [NSThread runInBackground:^{
        [_fmdbQueue inDatabase:^(FMDatabase *db) {
            [db open];
            NSTimeInterval time = (NSTimeInterval)[[NSDate date] timeIntervalSince1970];
            NSString * dateStr = [NSString getDateTimeInterval: time Format: @"yyyy-MM-dd" isMS: NO];
            NSString * sql = [NSString stringWithFormat: @"select * from myHistory where status = 1 and creatDate = '%@'",dateStr];
            FMResultSet * result = [db executeQuery: sql];
            NSMutableArray * dataSource = [NSMutableArray new];
            while ([result next])
            {
                NSDictionary * dic = [result resultDictionary];
                MoodObj * obj = [[MoodObj alloc] initWithDic: dic];
                [dataSource addObject: obj];
            }
            [result close];
            result = nil;
            [db close];
            
            [NSThread runInMain:^{
                success(@[dataSource]);
            }];
        }];
    }];
}
+ (NSArray *)getMoodColorList
{
    return @[RGB(132, 127, 127),RGB(48, 76, 249),RGB(62, 249, 185),RGB(248, 206, 58),RGB(255, 55, 229)];
}
+ (NSArray *)getMoodTitleList
{
    return @[STR(@"Extremely Sad"),STR(@"Sad"),STR(@"Commonly"),STR(@"Happy"),STR(@"Very Happy")];
}
+ (NSArray *)getMoodList
{
    return @[@"icon_feichangnanguo",@"icon_nanguo",@"icon_yiban",@"icon_kaixin",@"icon_henkaixin"];
}
+ (NSArray *)getWeatherList
{
    return @[@"sun_Image",@"duoyunzhuanqin",@"yintian",@"xiaoyu",@"zhongyu",@"dayu",@"zhenyu",@"leizhenyu",@"baoyu",@"yujiaxue",@"bingbao",@"xiaoxue",@"daxue",@"baoxue"];
}
+ (NSArray *)getWeatherNameList
{
    return @[STR(@"sun"),STR(@"Cloudy to sunny"),STR(@"Overcast"),STR(@"Light rain"),STR(@"Moderate rain"),STR(@"Heavy rain"),STR(@"Shower"),STR(@"Thunder shower"),STR(@"Rainstorm"),STR(@"Sleet"),STR(@"hail"),STR(@"Light snow"),STR(@"Heavy snow"),STR(@"Blizzard")];
}
@end
